﻿using System;
using System.Data;
using System.Collections.Generic;
using UniMana.Data;

namespace UniMana.Win.Training
{
    public class Cls_ConvertDataToScheduleDepartment
    {
        DBMain m_data = new DBMain();

        List<string> l_chunhat;
        List<string> l_thu2;
        List<string> l_thu3;
        List<string> l_thu4;
        List<string> l_thu5;
        List<string> l_thu6;
        List<string> l_thu7;
        DateTime start;
        DateTime end;
        private int positionEndTime = 0;

        int sessionlength1 = 0;
        int sessionlength2 = 0;
        int sessionlength3 = 0;
        int maxlength = 0;

        int m_GroupRow = 5;

        DataTable m_tblTableAllData;
        DataTable m_tblWeek1;
        DataTable m_tblWeek2;
        DataTable m_tblScheduleAll;
        DataTable m_tblScheduleWeek;

        public DataTable Implementation(DataTable m_tblTableAll)
        {
            m_tblScheduleAll = InitalScheduleTable(m_tblScheduleAll);
            m_tblScheduleAll = ReInitalScheduleTable(m_tblScheduleAll);
            m_tblScheduleWeek = InitalScheduleTable(m_tblScheduleWeek);
            m_tblTableAllData = new DataTable();
            this.m_tblTableAllData = m_tblTableAll;
            ResetValues();
            GetDataForWeek();
            // return thoi khoa bieu
            return m_tblScheduleAll;
        }

        /// <summary>
        /// Khởi tạo lại giá trị
        /// </summary>

        private void ResetValues()
        {
            // Table chứa dữ liệu 1 tuần
            m_tblWeek2 = InitalDataTableWeek(m_tblWeek2);            
            m_tblWeek1 = InitalDataTableWeek(m_tblWeek1);           
        }

        private void GetDataForWeek()
        {
            m_tblWeek1 = ReInitalDataTableWeek(m_tblWeek1);
            duyettheotuan:
            if (m_tblTableAllData.Rows.Count == 0)
                return;
            int _i = 0;
            DateTime _datetime = new DateTime();
            _datetime = (DateTime)(m_tblTableAllData.Rows[0]["RegistTime"]);
            start = new DateTime();
            end = new DateTime();
            m_tblWeek2 = ReInitalDataTableWeek(m_tblWeek2);
            ResetList();
            // Bắt đầu duyệt dữ liệu và xử lý sắp xếp dữ liệu theo từng tuần        
            while (m_tblTableAllData.Rows.Count != 0)
            {                
                if ((_i < int.Parse(m_tblTableAllData.Rows[0]["Training_WeekDayKey"].ToString())) ||((_i == int.Parse(m_tblTableAllData.Rows[0]["Training_WeekDayKey"].ToString())) && (_datetime == (DateTime)(m_tblTableAllData.Rows[0]["RegistTime"]))))
                {
                    _i = int.Parse(m_tblTableAllData.Rows[0]["Training_WeekDayKey"].ToString());
                    _datetime = (DateTime)(m_tblTableAllData.Rows[0]["RegistTime"]);
                    m_tblWeek2.Rows.Add(m_tblTableAllData.Rows[0]["Training_SessionKey"], m_tblTableAllData.Rows[0]["SessionName"], m_tblTableAllData.Rows[0]["SessionStartTime"], m_tblTableAllData.Rows[0]["SessionFinishTime"], m_tblTableAllData.Rows[0]["Training_Subject_ClassKey"], m_tblTableAllData.Rows[0]["ClassID"], m_tblTableAllData.Rows[0]["ClassName"], m_tblTableAllData.Rows[0]["Training_SubjectKey"], m_tblTableAllData.Rows[0]["SubjectID"], m_tblTableAllData.Rows[0]["SubjectName"], m_tblTableAllData.Rows[0]["Credit"], m_tblTableAllData.Rows[0]["Training_Class_RoomKey"], m_tblTableAllData.Rows[0]["RoomID"], m_tblTableAllData.Rows[0]["RoomName"], m_tblTableAllData.Rows[0]["Catalogue_TermKey"], m_tblTableAllData.Rows[0]["Catalogue_SchoolYearKey"], m_tblTableAllData.Rows[0]["Personnel_PersonnelKey"], m_tblTableAllData.Rows[0]["PersonID"], m_tblTableAllData.Rows[0]["PersonLastName"], m_tblTableAllData.Rows[0]["PersonName"], m_tblTableAllData.Rows[0]["RegistTime"], m_tblTableAllData.Rows[0]["Training_WeekDayKey"], m_tblTableAllData.Rows[0]["WeekDayName"]);
                    m_tblTableAllData.Rows.RemoveAt(0);
                }
                else
                    break;
            }

            // Xử lý dữ liệu từng tuần
            start = Convert.ToDateTime(m_tblWeek2.Rows[0]["RegistTime"].ToString());
            end = Convert.ToDateTime(m_tblWeek2.Rows[m_tblWeek2.Rows.Count - 1]["RegistTime"].ToString());
            // Xử lý dữ liệu và gom nhóm các tuần trùng dữ liệu
            if ( GroupScheduleWeek(ref m_tblWeek1, ref m_tblWeek2))
                goto duyettheotuan;
            // Xử lý đưa dữ liệu vào các list ngày từ thứ 2 đến thứ 7
            GetDataForList();
            // Xử lý đưa dữ liệu từ các list vào table thời khóa biểu cùng với các chi tiết tiêu đề thời gian
            GetDataInSchedule();
            // Lặp lại thao tác "Bắt đầu duyệt dữ liệu và xử lý sắp xếp dữ liệu theo từng tuần"
            if (m_tblTableAllData.Rows.Count > 0)
                goto duyettheotuan;
        }

        /// <summary>
        /// Xử lý dữ liệu và gom nhóm các tuần trùng dữ liệu
        /// </summary>
        /// <param name="week1"></param>
        /// <param name="week2"></param>
        /// <param name="point2"></param>
        /// <param name="end"></param>
        /// <returns></returns>

        private bool GroupScheduleWeek(ref DataTable week1, ref DataTable week2)
        {
            if (m_tblWeek1.Rows.Count != m_tblWeek2.Rows.Count)
            {
                ConvertTableToTable(ref m_tblWeek1, m_tblWeek2);
                return false;
            }
            if (m_tblWeek1.Rows.Count == m_tblWeek2.Rows.Count)
            {
                for (int i = 0; i < m_tblWeek2.Rows.Count; i++)
                {
                    if ((m_tblWeek1.Rows[i]["Training_SessionKey"].ToString() != m_tblWeek2.Rows[i]["Training_SessionKey"].ToString()) ||
                        (m_tblWeek1.Rows[i]["Training_Subject_ClassKey"].ToString() != m_tblWeek2.Rows[i]["Training_Subject_ClassKey"].ToString()) ||
                        (m_tblWeek1.Rows[i]["Training_SubjectKey"].ToString() != m_tblWeek2.Rows[i]["Training_SubjectKey"].ToString()) ||
                        (m_tblWeek1.Rows[i]["Credit"].ToString() != m_tblWeek2.Rows[i]["Credit"].ToString()) ||
                        (m_tblWeek1.Rows[i]["Training_Class_RoomKey"].ToString() != m_tblWeek2.Rows[i]["Training_Class_RoomKey"].ToString()) ||
                        (m_tblWeek1.Rows[i]["Catalogue_TermKey"].ToString() != m_tblWeek2.Rows[i]["Catalogue_TermKey"].ToString()) ||
                        (m_tblWeek1.Rows[i]["Catalogue_SchoolYearKey"].ToString() != m_tblWeek2.Rows[i]["Catalogue_SchoolYearKey"].ToString()) ||
                        (m_tblWeek1.Rows[i]["Training_WeekDayKey"].ToString() != m_tblWeek2.Rows[i]["Training_WeekDayKey"].ToString()))
                    {
                        ConvertTableToTable(ref m_tblWeek1, m_tblWeek2);
                        return false;
                    }
                }
            }
            if (week2.Rows.Count != 0)
            {
                m_tblScheduleAll.Rows[positionEndTime - 1][0] = end.Day + "-" + end.Month + "-" + end.Year;
                ConvertTableToTable(ref m_tblWeek1, m_tblWeek2);
                return true;
            }
            return false;
        }

        /// <summary>
        /// Sắp xếp dữ liệu từng tuần vào các List
        /// </summary>

        private void GetDataForList()
        {
            if (m_tblWeek2.Rows.Count != 0)
            {
                DataView dview = m_tblWeek2.DefaultView;
                dview.Sort = "Training_SessionKey ASC, Training_WeekDayKey ASC, Training_Class_RoomKey ASC";
                //dview.Sort = "";
                //dview.Sort = "";
                m_tblWeek2 = dview.ToTable();
                sessionlength1 = 0;
                sessionlength2 = 0;
                sessionlength3 = 0;

                int _balan = 0;
                for (int session = 1; session < 4; session++)    // Lặp lại 3 ca
                {
                    _balan = 0;
                    while (m_tblWeek2.Rows.Count != 0)
                    {
                        if (int.Parse(m_tblWeek2.Rows[0]["Training_SessionKey"].ToString()) == session)
                        {
                            #region [Chủ nhật]
                            if (int.Parse(m_tblWeek2.Rows[0]["Training_WeekDayKey"].ToString()) == 1)
                            {
                                if (l_chunhat.Count >= 5)
                                {
                                    if (m_tblWeek2.Rows[0]["RoomID"].ToString() == l_chunhat[l_chunhat.Count - 1].ToString())
                                    {
                                        // Xét tên lớp trùng
                                        if (m_tblWeek2.Rows[0]["ClassID"].ToString() != l_chunhat[l_chunhat.Count - 5].ToString())
                                            if (l_chunhat[l_chunhat.Count - 5].ToString() == "")
                                                l_chunhat[l_chunhat.Count - 5] = l_chunhat[l_chunhat.Count - 5] + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                            else
                                                l_chunhat[l_chunhat.Count - 5] = l_chunhat[l_chunhat.Count - 5] + " - " + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                        // Xét giảng viên trùng
                                        if (m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString() != l_chunhat[l_chunhat.Count - 2].ToString())
                                            l_chunhat[l_chunhat.Count - 2] = l_chunhat[l_chunhat.Count - 2] + m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString();
                                        goto finish;
                                    }
                                    else
                                        goto addnew;
                                }
                                else
                                    goto addnew;
                            addnew:
                                l_chunhat.Add(m_tblWeek2.Rows[0]["ClassID"].ToString());
                                l_chunhat.Add(m_tblWeek2.Rows[0]["SubjectName"].ToString());
                                l_chunhat.Add(m_tblWeek2.Rows[0]["Credit"].ToString());
                                l_chunhat.Add(m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString());
                                l_chunhat.Add(m_tblWeek2.Rows[0]["RoomID"].ToString());
                            finish:
                                string aa = "";
                            }
                            #endregion

                            #region [Thứ 2]
                            if (int.Parse(m_tblWeek2.Rows[0]["Training_WeekDayKey"].ToString()) == 2)
                            {
                                if (l_thu2.Count >= 5)
                                {
                                    if (m_tblWeek2.Rows[0]["RoomID"].ToString() == l_thu2[l_thu2.Count - 1].ToString())
                                    {
                                        // Xét tên lớp trùng
                                        if (m_tblWeek2.Rows[0]["ClassID"].ToString() != l_thu2[l_thu2.Count - 5].ToString())
                                            if (l_thu2[l_thu2.Count - 5].ToString() == "")
                                                l_thu2[l_thu2.Count - 5] = l_thu2[l_thu2.Count - 5] + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                            else
                                                l_thu2[l_thu2.Count - 5] = l_thu2[l_thu2.Count - 5] + " - " + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                        // Xét giảng viên trùng
                                        if (m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString() != l_thu2[l_thu2.Count - 2].ToString())
                                            l_thu2[l_thu2.Count - 2] = l_thu2[l_thu2.Count - 2] + m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString();
                                        string sdf = m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString();
                                        goto finish;
                                    }
                                    else
                                        goto addnew;
                                }
                                else
                                    goto addnew;
                            addnew:
                                l_thu2.Add(m_tblWeek2.Rows[0]["ClassID"].ToString());
                                l_thu2.Add(m_tblWeek2.Rows[0]["SubjectName"].ToString());
                                l_thu2.Add(m_tblWeek2.Rows[0]["Credit"].ToString());
                                l_thu2.Add(m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString());
                                l_thu2.Add(m_tblWeek2.Rows[0]["RoomID"].ToString());
                            finish:
                                string aa = "";
                            }
                            #endregion

                            #region [Thứ 3]
                            if (int.Parse(m_tblWeek2.Rows[0]["Training_WeekDayKey"].ToString()) == 3)
                            {
                                if (l_thu3.Count >= 5)
                                {
                                    if (m_tblWeek2.Rows[0]["RoomID"].ToString() == l_thu3[l_thu3.Count - 1].ToString())
                                    {
                                        // Xét tên lớp trùng
                                        if (m_tblWeek2.Rows[0]["ClassID"].ToString() != l_thu3[l_thu3.Count - 5].ToString())
                                            if (l_thu3[l_thu3.Count - 5].ToString() == "")
                                                l_thu3[l_thu3.Count - 5] = l_thu3[l_thu3.Count - 5] + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                            else
                                                l_thu3[l_thu3.Count - 5] = l_thu3[l_thu3.Count - 5] + " - " + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                        // Xét giảng viên trùng
                                        if (m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString() != l_thu3[l_thu3.Count - 2].ToString())
                                            l_thu3[l_thu3.Count - 2] = l_thu3[l_thu3.Count - 2] + m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString();
                                        goto finish;
                                    }
                                    else
                                        goto addnew;
                                }
                                else
                                    goto addnew;
                            addnew:
                                l_thu3.Add(m_tblWeek2.Rows[0]["ClassID"].ToString());
                                l_thu3.Add(m_tblWeek2.Rows[0]["SubjectName"].ToString());
                                l_thu3.Add(m_tblWeek2.Rows[0]["Credit"].ToString());
                                l_thu3.Add(m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString());
                                l_thu3.Add(m_tblWeek2.Rows[0]["RoomID"].ToString());
                            finish:
                                string aa = "";
                            }
                            #endregion

                            #region [Thứ 4]
                            if (int.Parse(m_tblWeek2.Rows[0]["Training_WeekDayKey"].ToString()) == 4)
                            {
                                if (l_thu4.Count >= 5)
                                {
                                    if (m_tblWeek2.Rows[0]["RoomID"].ToString() == l_thu4[l_thu4.Count - 1].ToString())
                                    {
                                        // Xét tên lớp trùng
                                        if (m_tblWeek2.Rows[0]["ClassID"].ToString() != l_thu4[l_thu4.Count - 5].ToString())
                                            if (l_thu4[l_thu4.Count - 5].ToString() == "")
                                                l_thu4[l_thu4.Count - 5] = l_thu4[l_thu4.Count - 5] + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                            else
                                                l_thu4[l_thu4.Count - 5] = l_thu4[l_thu4.Count - 5] + " - " + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                        // Xét giảng viên trùng
                                        if (m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString() != l_thu4[l_thu4.Count - 2].ToString())
                                            l_thu4[l_thu4.Count - 2] = l_thu4[l_thu4.Count - 2] + m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString();
                                        goto finish;
                                    }
                                    else
                                        goto addnew;
                                }
                                else
                                    goto addnew;
                            addnew:
                                l_thu4.Add(m_tblWeek2.Rows[0]["ClassID"].ToString());
                                l_thu4.Add(m_tblWeek2.Rows[0]["SubjectName"].ToString());
                                l_thu4.Add(m_tblWeek2.Rows[0]["Credit"].ToString());
                                l_thu4.Add(m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString());
                                l_thu4.Add(m_tblWeek2.Rows[0]["RoomID"].ToString());
                            finish:
                                string aa = "";
                            }
                            #endregion

                            #region [Thứ 5]
                            if (int.Parse(m_tblWeek2.Rows[0]["Training_WeekDayKey"].ToString()) == 5)
                            {
                                if (l_thu5.Count >= 5)
                                {
                                    if (m_tblWeek2.Rows[0]["RoomID"].ToString() == l_thu5[l_thu5.Count - 1].ToString())
                                    {
                                        // Xét tên lớp trùng
                                        if (m_tblWeek2.Rows[0]["ClassID"].ToString() != l_thu5[l_thu5.Count - 5].ToString())
                                            if (l_thu5[l_thu5.Count - 5].ToString() == "")
                                                l_thu5[l_thu5.Count - 5] = l_thu5[l_thu5.Count - 5] + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                            else
                                                l_thu5[l_thu5.Count - 5] = l_thu5[l_thu5.Count - 5] + " - " + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                        // Xét giảng viên trùng
                                        if (m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString() != l_thu5[l_thu5.Count - 2].ToString())
                                            l_thu5[l_thu5.Count - 2] = l_thu5[l_thu5.Count - 2] + m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString();
                                        goto finish;
                                    }
                                    else
                                        goto addnew;
                                }
                                else
                                    goto addnew;
                            addnew:
                                l_thu5.Add(m_tblWeek2.Rows[0]["ClassID"].ToString());
                                l_thu5.Add(m_tblWeek2.Rows[0]["SubjectName"].ToString());
                                l_thu5.Add(m_tblWeek2.Rows[0]["Credit"].ToString());
                                l_thu5.Add(m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString());
                                l_thu5.Add(m_tblWeek2.Rows[0]["RoomID"].ToString());
                            finish:
                                string aa = "";
                            }
                            #endregion

                            #region [Thứ 6]
                            if (int.Parse(m_tblWeek2.Rows[0]["Training_WeekDayKey"].ToString()) == 6)
                            {
                                if (l_thu6.Count >= 5)
                                {
                                    if (m_tblWeek2.Rows[0]["RoomID"].ToString() == l_thu6[l_thu6.Count - 1].ToString())
                                    {
                                        // Xét tên lớp trùng
                                        if (m_tblWeek2.Rows[0]["ClassID"].ToString() != l_thu6[l_thu6.Count - 5].ToString())
                                            if (l_thu6[l_thu6.Count - 5].ToString() == "")
                                                l_thu6[l_thu6.Count - 5] = l_thu6[l_thu6.Count - 5] + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                            else
                                                l_thu6[l_thu6.Count - 5] = l_thu6[l_thu6.Count - 5] + " - " + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                        // Xét giảng viên trùng
                                        if (m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString() != l_thu6[l_thu6.Count - 2].ToString())
                                            l_thu6[l_thu6.Count - 2] = l_thu6[l_thu6.Count - 2] + m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString();
                                        goto finish;
                                    }
                                    else
                                        goto addnew;
                                }
                                else
                                    goto addnew;
                            addnew:
                                l_thu6.Add(m_tblWeek2.Rows[0]["ClassID"].ToString());
                                l_thu6.Add(m_tblWeek2.Rows[0]["SubjectName"].ToString());
                                l_thu6.Add(m_tblWeek2.Rows[0]["Credit"].ToString());
                                l_thu6.Add(m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString());
                                l_thu6.Add(m_tblWeek2.Rows[0]["RoomID"].ToString());
                            finish:
                                string aa = "";
                            }
                            #endregion

                            #region [Thứ 7]
                            if (int.Parse(m_tblWeek2.Rows[0]["Training_WeekDayKey"].ToString()) == 7)
                            {
                                if (l_thu7.Count >= 5)
                                {
                                    if (m_tblWeek2.Rows[0]["RoomID"].ToString() == l_thu7[l_thu7.Count - 1].ToString())
                                    {
                                        // Xét tên lớp trùng
                                        if (m_tblWeek2.Rows[0]["ClassID"].ToString() != l_thu7[l_thu7.Count - 5].ToString())
                                            if (l_thu7[l_thu7.Count - 5].ToString() == "")
                                                l_thu7[l_thu7.Count - 5] = l_thu7[l_thu7.Count - 5] + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                            else
                                                l_thu7[l_thu7.Count - 5] = l_thu7[l_thu7.Count - 5] + " - " + m_tblWeek2.Rows[0]["ClassID"].ToString();
                                        // Xét giảng viên trùng
                                        if (m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString() != l_thu7[l_thu7.Count - 2].ToString())
                                            l_thu7[l_thu7.Count - 2] = l_thu7[l_thu7.Count - 2] + m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString();
                                        goto finish;
                                    }
                                    else
                                        goto addnew;
                                }
                                else
                                    goto addnew;
                            addnew:
                                l_thu7.Add(m_tblWeek2.Rows[0]["ClassID"].ToString());
                                l_thu7.Add(m_tblWeek2.Rows[0]["SubjectName"].ToString());
                                l_thu7.Add(m_tblWeek2.Rows[0]["Credit"].ToString());
                                l_thu7.Add(m_tblWeek2.Rows[0]["PersonLastName"].ToString() + " " + m_tblWeek2.Rows[0]["PersonName"].ToString());
                                l_thu7.Add(m_tblWeek2.Rows[0]["RoomID"].ToString());
                            finish:
                                string aa = "";
                            }
                            #endregion

                            // Xóa dòng xét
                            m_tblWeek2.Rows.RemoveAt(0);
                        }
                        else
                        {
                            _balan = 1;
                            BalanceData(session);
                            break;
                        }
                    }
                    // Cân bằng dữ liệu khi đã hết
                    if (_balan == 0)
                        BalanceData(session);
                }                
            }
        }

        /// <summary>
        /// Thêm danh sách lịch thời khóa biểu đã được xử lý thành tuần vào bảng thời khóa biếu
        /// </summary>

        private void GetDataInSchedule()
        {
            if (m_tblScheduleAll.Rows.Count != 0)
                m_tblScheduleAll.Rows.Add("", "", "", "", "", "", "", "", "");

            GetDataInScheduleWeek();

            if (m_tblScheduleWeek.Rows.Count > 0)
            {
                for (int i = 0; i < m_tblScheduleWeek.Rows.Count; i++)
                {
                    if (i == 5)
                        positionEndTime = m_tblScheduleAll.Rows.Count;
                    m_tblScheduleAll.Rows.Add(m_tblScheduleWeek.Rows[i][0],
                        m_tblScheduleWeek.Rows[i][1],
                        m_tblScheduleWeek.Rows[i][2],
                        m_tblScheduleWeek.Rows[i][3],
                        m_tblScheduleWeek.Rows[i][4],
                        m_tblScheduleWeek.Rows[i][5],
                        m_tblScheduleWeek.Rows[i][6],
                        m_tblScheduleWeek.Rows[i][7],
                        m_tblScheduleWeek.Rows[i][8]);
                }
            }
        }

        /// <summary>
        /// Xử lý dữ liệu từ danh sách các list thành table tuần
        /// </summary>

        private void GetDataInScheduleWeek()
        {
            m_tblScheduleWeek = ReInitalScheduleTable(m_tblScheduleWeek);
            m_tblScheduleWeek.Rows.Add("Ngày", "Buổi", "Thứ 2", "Thứ 3", "Thứ 4", "Thứ 5", "Thứ 6", "Thứ 7", "Chủ nhật");

            // Ca 1
            for (int i = 0; i < sessionlength1; i++)
            {
                m_tblScheduleWeek.Rows.Add("", "", "", "", "", "", "", "", "");
            }
            m_tblScheduleWeek.Rows[1]["Ngày"] = "Từ ngày";
            m_tblScheduleWeek.Rows[2]["Ngày"] = start.Day + "-" + start.Month + "-" + start.Year;
            m_tblScheduleWeek.Rows[3]["Ngày"] = "Đến ngày";
            m_tblScheduleWeek.Rows[4]["Ngày"] = end.Day + "-" + end.Month + "-" + end.Year;
            m_tblScheduleWeek.Rows[1]["Buổi"] = "Ca 1";
            m_tblScheduleWeek.Rows[2]["Buổi"] = "07:45";
            m_tblScheduleWeek.Rows[3]["Buổi"] = "11:00";
            for (int i = 0; i < sessionlength1; i++)
            {
                m_tblScheduleWeek.Rows[i + 1]["Thứ 2"] = l_thu2[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 3"] = l_thu3[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 4"] = l_thu4[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 5"] = l_thu5[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 6"] = l_thu6[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 7"] = l_thu7[i];
                m_tblScheduleWeek.Rows[i + 1]["Chủ nhật"] = l_chunhat[i];
            }

            // Ca 2
            for (int i = 0; i < sessionlength2 - sessionlength1; i++)
            {
                m_tblScheduleWeek.Rows.Add("", "", "", "", "", "", "", "", "");
            }
            m_tblScheduleWeek.Rows[sessionlength1 + 1]["Buổi"] = "Ca 2";
            m_tblScheduleWeek.Rows[sessionlength1 + 2]["Buổi"] = "12:45";
            m_tblScheduleWeek.Rows[sessionlength1 + 3]["Buổi"] = "16:00";
            for (int i = sessionlength1; i < sessionlength2; i++)
            {
                m_tblScheduleWeek.Rows[i + 1]["Thứ 2"] = l_thu2[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 3"] = l_thu3[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 4"] = l_thu4[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 5"] = l_thu5[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 6"] = l_thu6[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 7"] = l_thu7[i];
                m_tblScheduleWeek.Rows[i + 1]["Chủ nhật"] = l_chunhat[i];
            }

            // Ca 3
            for (int i = 0; i < sessionlength3 - sessionlength2; i++)
            {
                m_tblScheduleWeek.Rows.Add("", "", "", "", "", "", "", "", "");
            }
            m_tblScheduleWeek.Rows[sessionlength2 + 1]["Buổi"] = "Ca 3";
            m_tblScheduleWeek.Rows[sessionlength2 + 2]["Buổi"] = "16:30";
            m_tblScheduleWeek.Rows[sessionlength2 + 3]["Buổi"] = "19:45";
            for (int i = sessionlength2; i < sessionlength3; i++)
            {
                m_tblScheduleWeek.Rows[i + 1]["Thứ 2"] = l_thu2[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 3"] = l_thu3[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 4"] = l_thu4[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 5"] = l_thu5[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 6"] = l_thu6[i];
                m_tblScheduleWeek.Rows[i + 1]["Thứ 7"] = l_thu7[i];
                m_tblScheduleWeek.Rows[i + 1]["Chủ nhật"] = l_chunhat[i];
            }
        }

        /// <summary>
        /// Thiết lập lại danh sách các list
        /// </summary>

        private void ResetList()
        {
            l_chunhat = new List<string>();
            l_thu2 = new List<string>();
            l_thu3 = new List<string>();
            l_thu4 = new List<string>();
            l_thu5 = new List<string>();
            l_thu6 = new List<string>();
            l_thu7 = new List<string>();
        }

        /// <summary>
        /// Điều khiển xử lý cân bằn dữ liệu các list
        /// </summary>
        /// <param name="session"></param>

        private void BalanceData(int session)
        {
            maxlength = 0;
            if (l_chunhat.Count > maxlength) maxlength = l_chunhat.Count;
            if (l_thu2.Count > maxlength) maxlength = l_thu2.Count;
            if (l_thu3.Count > maxlength) maxlength = l_thu3.Count;
            if (l_thu4.Count > maxlength) maxlength = l_thu4.Count;
            if (l_thu5.Count > maxlength) maxlength = l_thu5.Count;
            if (l_thu6.Count > maxlength) maxlength = l_thu6.Count;
            if (l_thu7.Count > maxlength) maxlength = l_thu7.Count;
            if (session == 1)
            {
                if (maxlength == 0)
                {
                    FillLists(4);
                    sessionlength1 = 4;
                }
                else
                {
                    FillLists(maxlength);
                    sessionlength1 = maxlength;
                }
            }
            if (session == 2)
            {
                if (maxlength == sessionlength1)
                {
                    FillLists(sessionlength1 + 4);
                    sessionlength2 = sessionlength1 + 4;
                }
                else
                {
                    FillLists(maxlength);
                    //sessionlength2 = sessionlength1 + maxlength;
                    sessionlength2 = maxlength;
                }
            }
            if (session == 3)
            {
                if (maxlength == sessionlength2)
                {
                    FillLists(sessionlength2 + 4);
                    sessionlength3 = sessionlength2 + 4;
                }
                else
                {
                    FillLists(maxlength);
                    //sessionlength3 = sessionlength2 + maxlength;
                    sessionlength3 = maxlength;
                }
            }
        }

        /// <summary>
        /// Cân bằng giá trị các list
        /// </summary>
        /// <param name="liststring"></param>
        /// <param name="mlength"></param>

        private void FillLists(int mlength)
        {
            FillList(ref l_chunhat, mlength);
            FillList(ref l_thu2, mlength);
            FillList(ref l_thu3, mlength);
            FillList(ref l_thu4, mlength);
            FillList(ref l_thu5, mlength);
            FillList(ref l_thu6, mlength);
            FillList(ref l_thu7, mlength);
        }

        /// <summary>
        /// Cân bằng giá trị list
        /// </summary>
        /// <param name="liststring"></param>
        /// <param name="mlength"></param>

        private void FillList(ref List<string> liststring, int mlength)
        {
            if (mlength > liststring.Count)
                for (int i = liststring.Count; i < mlength; i++)
                {
                    liststring.Add("");
                }
        }

        /// <summary>
        /// Chuyển dữ liệu từ table week2 sang week1
        /// </summary>
        /// <param name="week1"></param>
        /// <param name="week2"></param>

        private void ConvertTableToTable(ref DataTable week1, DataTable week2)
        {
            week1 = ReInitalDataTableWeek(week1);
            for (int i = 0; i < week2.Rows.Count; i++)
            {
                week1.Rows.Add(week2.Rows[i][0], week2.Rows[i][1], week2.Rows[i][2], week2.Rows[i][3], week2.Rows[i][4], week2.Rows[i][5], week2.Rows[i][6], week2.Rows[i][7], week2.Rows[i][8], week2.Rows[i][9], week2.Rows[i][10], week2.Rows[i][11], week2.Rows[i][12], week2.Rows[i][13], week2.Rows[i][14], week2.Rows[i][15], week2.Rows[i][16], week2.Rows[i][17], week2.Rows[i][18], week2.Rows[i][19], week2.Rows[i][20], week2.Rows[i][21], week2.Rows[i][22]);
            }
        }

        /// <summary>
        /// Khởi tạo lại table chứa dữ liệu từng tuần
        /// </summary>
        /// <param name="m_tblWeek"></param>
        /// <returns></returns>

        private DataTable ReInitalDataTableWeek(DataTable m_tblWeek)
        {
            m_tblWeek.Columns.Clear();
            m_tblWeek.Rows.Clear();
            m_tblWeek = InitalDataTableWeek(m_tblWeek);
            return m_tblWeek;
        }

        /// <summary>
        /// Khởi tạo table chứa dữ liệu từng tuần
        /// </summary>
        /// <param name="m_tblWeek"></param>
        /// <returns></returns>

        private DataTable InitalDataTableWeek(DataTable m_tblWeek)
        {
            m_tblWeek = new DataTable();
            m_tblWeek.Columns.Add("Training_SessionKey");
            m_tblWeek.Columns.Add("SessionName");
            m_tblWeek.Columns.Add("SessionStartTime");
            m_tblWeek.Columns.Add("SessionFinishTime ");
            m_tblWeek.Columns.Add("Training_Subject_ClassKey");
            m_tblWeek.Columns.Add("ClassID");
            m_tblWeek.Columns.Add("ClassName");
            m_tblWeek.Columns.Add("Training_SubjectKey");
            m_tblWeek.Columns.Add("SubjectID");
            m_tblWeek.Columns.Add("SubjectName");
            m_tblWeek.Columns.Add("Credit");
            m_tblWeek.Columns.Add("Training_Class_RoomKey");
            m_tblWeek.Columns.Add("RoomID");
            m_tblWeek.Columns.Add("RoomName");
            m_tblWeek.Columns.Add("Catalogue_TermKey");
            m_tblWeek.Columns.Add("Catalogue_SchoolYearKey");
            m_tblWeek.Columns.Add("Personnel_PersonnelKey");
            m_tblWeek.Columns.Add("PersonID");
            m_tblWeek.Columns.Add("PersonLastName");
            m_tblWeek.Columns.Add("PersonName");
            m_tblWeek.Columns.Add("RegistTime");
            m_tblWeek.Columns.Add("Training_WeekDayKey");
            m_tblWeek.Columns.Add("WeekDayName");
            return m_tblWeek;
        }

        /// <summary>
        /// Khởi tạo lại table chứa lịch công tác từng tuần
        /// </summary>
        /// <param name="m_tbl"></param>
        /// <returns></returns>

        private DataTable ReInitalScheduleTable(DataTable m_tbl)
        {
            m_tbl.Columns.Clear();
            m_tbl.Rows.Clear();
            m_tbl = InitalScheduleTable(m_tbl);
            return m_tbl;
        }

        /// <summary>
        /// Khởi tạo table chứa lịch công tác từng tuần
        /// </summary>
        /// <param name="m_tbl"></param>
        /// <returns></returns>

        private DataTable InitalScheduleTable(DataTable m_tbl)
        {
            m_tbl = new DataTable();
            m_tbl.Columns.Add("Ngày");
            m_tbl.Columns.Add("Buổi");
            m_tbl.Columns.Add("Thứ 2");
            m_tbl.Columns.Add("Thứ 3");
            m_tbl.Columns.Add("Thứ 4");
            m_tbl.Columns.Add("Thứ 5");
            m_tbl.Columns.Add("Thứ 6");
            m_tbl.Columns.Add("Thứ 7");
            m_tbl.Columns.Add("Chủ nhật");
            return m_tbl;
        }
    }
}
